/**
 * @author Samuel Gonzalez
 * @version 1.0.6 Thu May 10, 2012 10:04PM GMT-400
 */

/*
 * Copyright (C) 2011  Samuel Gonzalez, PR
 * BenchGraph. Generates graphics and HTML tables based on HTTPERF output.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * In addition, as a special exception, the copyright holders give permission
 * to link the code of this work with the OpenSSL project's "OpenSSL" library
 * (or with modified versions of it that use the same license as the "OpenSSL" 
 * library), and distribute linked combinations including the two.  You must
 * obey the GNU General Public License in all respects for all of the code
 * used other than "OpenSSL".  If you modify this file, you may extend this
 * exception to your version of the file, but you are not obligated to do so.
 * If you do not wish to do so, delete this exception statement from your
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
 */

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class Bar {
	
	private final String IMGNAME = "NO NAME";
	
	// Attributes
	private String imgName; 				// Hold image name
	private DefaultCategoryDataset dataset; // Hold data
	private JFreeChart chart;		// Hold chart
	
	/**
	 *  Assign attributes default values
	 */
	public Bar(){
		this.imgName = this.IMGNAME;
		this.dataset = null;
		this.chart = null;
	}
	
	/**
	 * Assign attribute received values and
	 * call method to create charts
	 * @param directory
	 * @param imgName
	 * @param data
	 */
	public Bar(String directory, String imgName, ArrayList<String> data){
		setImgName(imgName);
		createChart(directory, data);
	}
	
	/**
	 * Return image name
	 * @return imgName
	 */
	public String getImgName(){ // Getter
		return this.imgName;
	}
	
	/**
	 * Set image name
	 * @param name
	 */
	public void setImgName(String name){
		this.imgName = (name != null)? name : this.IMGNAME;
	}
	
	/**
	 * Create and export chart
	 * @param directory
	 * @param data
	 */
	private void createChart(String directory, ArrayList<String> data){
		this.dataset = new DefaultCategoryDataset();
		
		for(int i = 1; i < data.size(); i+=2)
			this.dataset.addValue(Float.parseFloat(data.get(i + 1)), data.get(i), "");
		
		this.chart = ChartFactory.createBarChart(data.get(0), "", "", this.dataset, PlotOrientation.VERTICAL, true, false, false);
		
		try{
			data.set(0, data.get(0).replace("/", " per "));
			this.imgName += "-" + data.get(0) + ".png";
			ChartUtilities.saveChartAsPNG(new File(directory + this.imgName), this.chart, 400, 350);
		}catch(IOException e){
			Console.wln(this.imgName + " ...........................................[fail]");
		}
	}
}

